<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\StoreCompanyInfo;
use App\Http\Requests\UpdateCompanyInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class CompanyInfo extends Controller
{
    //获取我的公司信息
    public function index()
    {
        $data = \App\CompanyInfo::where('uid', Auth::id())->first();
        if ($data) {
            if ($data->image) {
                $data->image = explode(',', $data->image);
            }
        }
        return $data;
    }


    //创建/修改公司信息
    public function update(UpdateCompanyInfo $request, \App\CompanyInfo $companyInfo)
    {
        $info = $companyInfo::where('uid', Auth::id())->first();
        if ($info) {//修改
            $data = $request->except(['image']);
            if ($request->image) {
                $image = implode(',', $request->image);
                if (strlen($image > 1000)) {
                    return response(['message' => '图片过多'], 400);
                }
                $data['image'] = $image;
            }
            $res = $companyInfo::where('uid', Auth::id())->update($data);
            if ($res) {
                return response(['message' => '修改成功']);
            }
            return response(['message' => '修改失败'], 400);
        } else {//创建
            $data = ['uid' => Auth::id()];
            if ($request->image) {
                $image = implode(',', $request->image);
                if (strlen($image > 1000)) {
                    return response(['message' => '图片过多'], 400);
                }
                $data['image'] = $image;
            }
            $res = $companyInfo::create(array_merge($request->only([
                'logo',
                'cname',
                'hangye',
                'renshu',
                'xingzhi',
                'jianjie',
                'province',
                'city',
                'area',
                'detailed_address',
                'video',
                'music',
                'phone',
            ]), $data));
            if ($res) {
                return response(['message' => '添加成功']);
            }
            return response(['message' => '添加失败'], 400);
        }
    }

    public function flauntDelete(Request $request)
    {
        $user_id = Auth::id();
        $type = $request->type;//image  video  audio
        $path = $request->path;
        $info = \App\CompanyInfo::where('uid', $user_id)->first();
        if ($type == 'image') {
            $data = explode(',', $info->image);
            foreach ($data as $k => $v) {
                if ($v == $path) {
                    unset($data[$k]);
                    //只是删除了数据库，图片并未删除
                }
            }
            if ($data) {
                $data = implode(',', $data);
            } else {
                $data = null;
            }
            $info->image = $data;
            $res = $info->save();
            if ($res) {
                return response(['message' => '删除成功']);
            }
            return response(['message' => '删除失败'], 400);
        } elseif ($type == 'video') {
            $info->video = null;
            $res = $info->save();
            //只是删除了数据库，视频并未删除
            if ($res) {
                return response(['message' => '删除成功']);
            }
            return response(['message' => '删除失败'], 400);
        } elseif ($type == 'audio') {
            $info->music = null;
            $res = $info->save();
            //只是删除了数据库，视频并未删除
            if ($res) {
                return response(['message' => '删除成功']);
            }
            return response(['message' => '删除失败'], 400);
        } else {
            return response(['message' => '删除失败'], 400);
        }
    }

    //图片或视频音频删除
    public function flauntDeletes(Request $request)
    {
        $type = $request->type;//image  video music
        $info = \App\CompanyInfo::where('uid', Auth::id())->first();
        if ($type == 'image') {
            $info->image = '';
        } elseif ($type == 'video') {
            $info->video = '';
        } elseif ($type == 'music') {
            $info->music = '';
        } else {
            return response(['message' => '删除失败'], 400);
        }
        $info->save();
        return response(['message' => '删除成功']);
    }
}
